package cn.unuuc.cwl.account.service;

import cn.unuuc.cwl.account.entity.Account;
import cn.unuuc.cwl.account.mapper.AccountMapper;
import cn.unuuc.cwl.account.model.qo.AccountQo;
import cn.unuuc.cwl.common.exception.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;


@Service
public class AccountService extends ServiceImpl<AccountMapper, Account> {


    public void deductMoney(AccountQo accountQo) {
        Account account = this.baseMapper.selectOne(new LambdaQueryWrapper<Account>()
                .eq(Account::getAccount, accountQo.getAccount()));
        if (account == null) {
            throw new ServiceException("账号不存在");
        }
        if ((account.getBalance() - accountQo.getDeductMoney()) < 0) {
            throw new ServiceException("余额不足");
        }
        account.setBalance(account.getBalance() - accountQo.getDeductMoney());
        this.updateById(account);
    }

}
